package com.hh.controller;

import com.hh.dto.ResultDTO;
import com.hh.service.TypeService;
import com.hh.until.Tool;
import com.hh.vo.TypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/back/newsType")
public class NewsTypeController {
    @Autowired
    @Qualifier(value = "newsTypeServiceImpl")
    TypeService newsTypeService;

    @GetMapping("/findAll")
    public ResultDTO findAll(TypeVO typeVO) {
        typeVO.setEnable(Tool.nullToInt(typeVO.getEnable()));
        if (typeVO.getPage() != null && typeVO.getLimit() != null) {
            typeVO.setPage(Tool.strToPage(typeVO.getPage().toString()));
            typeVO.setLimit(Tool.strToLimit(typeVO.getLimit().toString()));
        }
        ResultDTO resultDTO = newsTypeService.findAll(typeVO);
        return resultDTO;
    }

    @GetMapping("/findById")
    public ResultDTO findById(TypeVO typeVO) {
        typeVO.setId(Tool.strToInt(Tool.nullToStr(typeVO.getId().toString())));

        ResultDTO resultDTO = newsTypeService.findById(typeVO);
        return resultDTO;
    }

    @PostMapping("/add")
    public ResultDTO add(TypeVO typeVO) {
        typeVO.setName(Tool.nullToStr(typeVO.getName()));
        typeVO.setEnable(Tool.nullToInt(typeVO.getEnable()));

        ResultDTO resultDTO = newsTypeService.add(typeVO);
        return resultDTO;
    }

    @PostMapping("/update")
    public ResultDTO update(TypeVO typeVO) {
        typeVO.setId(Tool.strToInt(typeVO.getId().toString()));
        typeVO.setName(Tool.nullToStr(typeVO.getName()));
        typeVO.setEnable(Tool.nullToInt(typeVO.getEnable()));

        ResultDTO resultDTO = newsTypeService.update(typeVO);
        return resultDTO;
    }

    @PostMapping("/delete")
    public ResultDTO delete(TypeVO typeVO) {
        typeVO.setId(Tool.strToInt(typeVO.getId().toString()));

        ResultDTO resultDTO = newsTypeService.delete(typeVO);
        return resultDTO;
    }


}
